package plugin;

import java.awt.Component;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Calendar;
import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.Timer;

public class Clock extends JPanel {
	private static final long serialVersionUID = 1L;
	private JLabel stunden;
	private JLabel minuten;
	private ConnectionManager connection;
	private Timer refreshTimer;
	private JLabel sekunden;
	private long time;
	private long starttime = 0;
	private String start_realtime;

	protected Clock(ConnectionManager connection) {
		this.setLayout(new BoxLayout(this, BoxLayout.LINE_AXIS));
		this.stunden = new JLabel("XX");
		this.minuten = new JLabel("XX");
		this.sekunden = new JLabel("XX");
		this.connection = connection;
		Font font = new Font("Courier", Font.BOLD, 12);
		this.add(new JLabel("Uhrzeit: "));
		this.add(stunden);
		this.add(new JLabel(":"));
		this.add(minuten);
		this.add(new JLabel(":"));
		this.add(sekunden);
		this.add(Box.createHorizontalGlue());
		for (Component c : getComponents()) {
			c.setFont(font);
		}
		addRefreshTimer();
		this.setBorder(BorderFactory.createEmptyBorder(0, 10, 0, 0));
	}

	private void addRefreshTimer() {
		this.refreshTimer = new Timer(1000, new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent evt) {
				setTime(connection.getSimutime());
			}
		});
		refreshTimer.start();
	}
	
	protected String getStartTime() {
		return stunden(starttime)+":"+minuten(starttime);
	}
	
	protected String getTotalTime() {
		long totaltime = time-starttime+59;
		return stunden(totaltime)+":"+minuten(totaltime);
	}
	
	protected String getEndTime() {
		return  stunden(time)+":"+minuten(time);
	}
	
	protected String getRealCurrentTime() {
		Calendar zeit = Calendar.getInstance();
		zeit.setTimeInMillis(System.currentTimeMillis());
		String stunden = ""+zeit.get(Calendar.HOUR_OF_DAY);
		String minuten = ""+zeit.get(Calendar.MINUTE);
		if(stunden.length()==1) stunden="0"+stunden;
		if(minuten.length()==1) minuten="0"+minuten;
		return  stunden+":"+minuten;
	}
	
	protected String getRealStartTime() {		
		return this.start_realtime;
	}

	protected void setTime(long t) {
		this.time = t / 1000;
		if (this.starttime==0) {
			this.starttime=this.time;			
			Calendar zeit = Calendar.getInstance();
			zeit.setTimeInMillis(System.currentTimeMillis());
			String stunden = ""+zeit.get(Calendar.HOUR_OF_DAY);
			String minuten = ""+zeit.get(Calendar.MINUTE);
			if(stunden.length()==1) stunden="0"+stunden;
			if(minuten.length()==1) minuten="0"+minuten;		
			this.start_realtime = 	stunden+":"+minuten;
		}
		
		if (!sekunden.getText().equals(sekunden(time))) {
			stunden.setText(stunden(time));
			minuten.setText(minuten(time));
			sekunden.setText(sekunden(time));
			this.revalidate();
			this.repaint();
		}
		if(connection.getSimutime()<10000 || (connection.getSimutime()>86400000 && connection.getUi().getMainUI().connected)) {
			refreshTimer.stop();
			SwingUtilities.invokeLater(new Runnable() {
				public void run() {
					connection.reset();
				}
			});
		}
	}

	private String minuten(long time) {
		String tmp;
		tmp = "" + ((int) ((time / 60)) % 60);
		if (("" + ((int) ((time / 60) % 60))).length() == 1)
			tmp = "0" + tmp;
		return tmp;
	}

	private String stunden(long time) {
		String tmp;
		tmp = "" + ((int) ((time / 60 / 60)));
		if (("" + ((int) ((time / 60 / 60)))).length() == 1)
			tmp = "0" + tmp;
		return tmp;
	}

	private String sekunden(long time) {
		String tmp;
		tmp = "" + time % 60;
		if (("" + time % 60).length() == 1)
			tmp = "0" + tmp;
		return tmp;
	}

	protected boolean isReached(long time) {
		return time <= this.time;
	}

	/**
	 * return die Zeit in Minuten bis zur als Param gelieferten Zeit
	 * 
	 * @param time
	 *            Zeit in Sekunden ab 00:00
	 * @return
	 */
	protected int getMinutesToTime(long time) {
		return (int) ((time / 60) - (this.time / 60));
	}	

	protected long getTime() {
		return time;
	}
	protected String getTimeString() {
//		Calendar zeit = Calendar.getInstance();
//		zeit.setTimeInMillis(System.currentTimeMillis());
		String stunden = ""+stunden(time);
		String minuten = ""+minuten(time);
		if(stunden.length()==1) stunden="0"+stunden;
		if(minuten.length()==1) minuten="0"+minuten;		
		return stunden+":"+minuten;
	}

}
